package com.easy.stock.spider.html;

import java.io.IOException;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import com.easy.stock.dao.StockEarningDao;
import com.easy.stock.dao.StockInfoDao;
import com.easy.stock.dao.StockInsideTradeDao;
import com.easy.stock.dao.StockRateDao;
import com.easy.stock.domain.StockEarning;
import com.easy.stock.domain.StockInfo;
import com.easy.stock.domain.StockInsideTrade;
import com.easy.stock.domain.StockRate;
import com.easy.stock.spider.BaseAsyncCompletionHandler;
import com.easy.stock.spider.SpiderTask;
import com.easy.stock.spider.SpiderTask.STATUS;
import com.ning.http.client.Response;

public class ARNSpider extends BaseHtmlSpider {
	
	@Autowired
	private StockInfoDao stockInfoDao;
	
	@Autowired
	private StockRateDao stockRateDao;
	
	@Autowired
	private StockInsideTradeDao stockInsideTradeDao;
	
	@Autowired
	private StockEarningDao stockEarningDao;
	
	private String baseUrl;

	@Override
	protected List<SpiderTask> prepareSeeds() {
		
		List<SpiderTask> tasks = new ArrayList<SpiderTask>();
		List<StockInfo> stocks = null;
		int limit=1000;
		int offset=0;
		while(!(stocks=stockInfoDao.getStockInfoList(offset, limit)).isEmpty()) {
			for(StockInfo stock : stocks) {
				SpiderTask task = new SpiderTask(baseUrl+stock.getId(), new ARNHandler(this, stock.getId()), STATUS.READY);
				tasks.add(task);
			}
			
			offset+=limit;
		}
		return tasks;
	
	}
	
	public static class ARNHandler extends BaseAsyncCompletionHandler {
		
		private String stockId;
		
		public ARNHandler(BaseHtmlSpider spider, String stockId) {
			super.spider = spider;
			this.stockId = stockId;
		}

		@Override
		public List<SpiderTask> doOnCompleted(Response response)
				throws IOException {
			
			ARNSpider mySpider = (ARNSpider)spider;
			String content = response.getResponseBody("UTF-8");
			
			Document doc = Jsoup.parse(content);
			SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
			// step1, 解析评级信息
			Elements elems = doc.select("#cphPrimaryContent_TabContainer1_TabPanel1 #ratingstable tbody tr");
			int elemNum = elems.size();
			for(int i=0; i<elemNum; i++) {
				Element elem = elems.get(i);
				
				StockRate rateItem = new StockRate();
				try {
					rateItem.setTime(sdf.parse(elem.child(0).html().trim()));
				} catch (ParseException e) {
					throw new RuntimeException("can't parse rate time");
				}
				rateItem.setFirm(StringEscapeUtils.unescapeJava(elem.child(1).html().trim()));
				rateItem.setAction(StringEscapeUtils.unescapeJava(elem.child(2).html().trim()));
				rateItem.setRating(StringEscapeUtils.unescapeJava(elem.child(3).html().trim()));
				rateItem.setTarget_price(StringEscapeUtils.unescapeJava(StringEscapeUtils.unescapeHtml4(elem.child(4).html().trim())));
				rateItem.setStock_id(stockId);
				mySpider.stockRateDao.addRate(rateItem);
			}
			
			// step2, 解析财报
			elems = doc.select("#cphPrimaryContent_TabContainer1_TabPanel2 #ratingstable tbody tr");
			elemNum = elems.size();
			for(int i=0; i<elemNum; i++) {
				Element elem = elems.get(i);
				
				StockEarning earningItem = new StockEarning();
				try {
					earningItem.setTime(sdf.parse(elem.child(0).html().trim()));
				} catch (ParseException e) {
					throw new RuntimeException("can't parse rate time");
				}
				earningItem.setQuarter(StringEscapeUtils.unescapeJava(elem.child(1).html().trim()));
				earningItem.setConsensus_estimate(StringEscapeUtils.unescapeJava(elem.child(2).html().trim()));
				earningItem.setActual_eps(StringEscapeUtils.unescapeJava(elem.child(3).html().trim()));
				earningItem.setRevenue_estimate(StringEscapeUtils.unescapeJava(elem.child(4).html().trim()));
				earningItem.setActual_revenue(StringEscapeUtils.unescapeJava(elem.child(5).html().trim()));
				earningItem.setStock_id(stockId);
				mySpider.stockEarningDao.addEarning(earningItem);
			}
			
			// step3, 解析内部交易
			elems = doc.select("#cphPrimaryContent_TabContainer1_TabPanel4 #ratingstable tbody tr");
			elemNum = elems.size();
			for(int i=0; i<elemNum; i++) {
				Element elem = elems.get(i);
				
				StockInsideTrade tradeItem = new StockInsideTrade();
				try {
					tradeItem.setTime(sdf.parse(elem.child(0).html().trim()));
				} catch (ParseException e) {
					throw new RuntimeException("can't parse rate time");
				}
				tradeItem.setInsider_name(StringEscapeUtils.unescapeJava(elem.child(1).html().trim()));
				tradeItem.setTitle(StringEscapeUtils.unescapeJava(elem.child(2).html().trim()));
				tradeItem.setAction(StringEscapeUtils.unescapeJava(elem.child(3).html().trim()));
				tradeItem.setShares(StringEscapeUtils.unescapeJava(elem.child(4).html().trim()));
				tradeItem.setPrice(StringEscapeUtils.unescapeJava(elem.child(5).html().trim()));
				tradeItem.setTotal(StringEscapeUtils.unescapeJava(elem.child(6).html().trim()));
				tradeItem.setStock_id(stockId);
				mySpider.stockInsideTradeDao.addTrade(tradeItem);
			}
			
			return null;
		}
		
		@Override
		public void doOnThrowable(Throwable t) {
			
		}
	}
	
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

}
